/*************************************************************************
	> File Name: fun.c
	> Author: ycst
	> Mail: a1277992967@163.com 
	> blog: http://www.ycst.club/suggest/
	> Created Time: 2022/7/4 9:24:30
 ************************************************************************/
#include"fun.h"

void Welcome() {
	puts("**************************\n");
	puts("* Welcome to Gobang game *\n");
	puts("**************************\n");


	puts("Start Game PVP : 1\n");
	puts("Start Game PVE_Hard : 2\n");
	puts("Start Game PVE_Easy : 3\n");
	puts("Quit Game : 4 or else\n");
	puts("\n");
}

void Good_Bye(){
	puts("**************************\n");
	puts("*         GoodBYe        *\n");
	puts("**************************\n");
}

void Init(int *arr) {
	int(*p)[HEIGHT] = arr;
	for (int i = 0; i < WIDTH; i++) {
		for (int j = 0; j < HEIGHT; j++) {
			p[i][j] = 95;
		}
	}
}

void Print(int *arr){
	int (*p)[HEIGHT] = arr;
	printf("\t");
	for (int m = 0; m < WIDTH; m++) {
		printf("%d ", m );
	}
	printf("\n");
	for (int i = 0; i < WIDTH; i++) {
		printf(" %d\t", i);
		for (int j = 0; j < HEIGHT; j++) {
			printf("%c ", p[i][j]);
		}	
		printf("\n");
	}
	printf("\n");
}

void Input_Man1(int *arr, int x, int y) {
	int(*p)[HEIGHT] = arr;
	p[x][y] = 'x';
}

void Input_Man2(int *arr, int x, int y) {
	int(*p)[HEIGHT] = arr;
	p[x][y] = '*';
}

void Input_Rebot_Hard(int *arr) {
	int(*p)[15] = arr;
	int x = 0, y = 0;
	int time = 0;
	int transverse = 0, transverse_max = transverse;
	int vertical = 0, vertical_max = 0;
	int oblique_right = 0, oblique_right_max = 0;
	int oblique_left = 0, oblique_left_max = 0;
	time = 0 + rand() % (3 - 0 + 1);
	Sleep(time * 1000);

	for (int i = 0; i < WIDTH; i++) {
		for (int j = 0; j < HEIGHT; j++) {
			if (p[i][j] == 'x') {
				if (p[i + 1][j] == 'x') {
					for (transverse = 0; transverse < 5; transverse++) {
						if (p[i + transverse][j] != 'x')break;
					}
					if (transverse_max < transverse) {
						transverse_max = transverse;
						x = i;
						y = j;
					}
				}
				if (p[i][j + 1] == 'x') {
					for (vertical = 0; vertical < 5; vertical++) {
						if (p[i][j + vertical] != 'x')break;
					}
					if (vertical_max < vertical) {
						vertical_max = vertical;
						x = i;
						y = j;
					}
				}
				if (p[i + 1][j + 1] == 'x') {
					for (oblique_right = 0; oblique_right < 5; oblique_right++) {
						if (p[i + oblique_right][j + oblique_right] != 'x')break;
					}
					if (oblique_right_max < oblique_right) {
						oblique_right_max = oblique_right;
						x = i;
						y = j;
					}
				}
				if (p[i + 1][j - 1] == 'x') {
					for (oblique_left = 0; oblique_left < 5; oblique_left++) {
						if (p[i + oblique_left][j + oblique_left] != 'x')break;
					}
					if (oblique_left_max < oblique_left) {
						oblique_left_max = oblique_left;
						x = i;
						y = j;
					}
				}

			}
		}
}

	if (max(max(transverse_max, vertical_max), max(oblique_right_max, oblique_left_max)) == transverse_max) {
		for (transverse = 0; transverse < 5; transverse++) {
			if (p[x + transverse][y] == '_') {
				break;
			}
		}
		int Head = Three_times_Three(p, x - 1, y);
		int Tail = Three_times_Three(p, x + transverse, y);
		if (Head >= Tail && p[x - 1][y] == '_')p[x - 1][y] = '@';
		else p[x + transverse][y] = '@';
			
	}
	else if (max(max(transverse_max, vertical_max), max(oblique_right_max, oblique_left_max)) == vertical_max) {
		for (vertical = 0; vertical < 5; vertical++) {
			if (p[x][y + vertical] == '_') {
				break;
			}
		}
		int Head = Three_times_Three(p, x, y - 1);
		int Tail = Three_times_Three(p, x, y + vertical);
		if (Head >= Tail && p[x][y - 1] == '_')p[x][y - 1] = '@';
		else p[x][y + vertical] = '@';

	}
	else if (max(max(transverse_max, vertical_max), max(oblique_right_max, oblique_left_max)) == oblique_right_max) {
		for (oblique_right = 0; oblique_right < 5; oblique_right++) {
			if (p[x + oblique_right][y + oblique_right] == '_') {
				break;
			}
		}
		int Head = Three_times_Three(p, x - 1, y - 1);
		int Tail = Three_times_Three(p, x + oblique_right, y + oblique_right);
		if (Head >= Tail && p[x - 1][y - 1] == '_')p[x - 1][y - 1] = '@';
		else p[x + oblique_right][y + oblique_right] = '@';

	}
	else {
		for (oblique_left = 0; oblique_left < 5; oblique_left++) {
			if (p[x + oblique_left][y - oblique_left] == '_') {
				break;
			}
		}
		int Head = Three_times_Three(p, x - 1, y + 1);
		int Tail = Three_times_Three(p, x + oblique_left, y + oblique_left);
		if (Head >= Tail && p[x - 1][y + 1] == '_')p[x - 1][y + 1] = '@';
		else p[x + oblique_left][y - oblique_left] = '@';
	}
}

void Input_Rebot_Easy(int *arr) {
	int(*p)[HEIGHT] = arr;
	int x = 0, y = 0;
	int time = 0 + rand() % (3 - 0 + 1);
	Sleep(time * 1000);
	while (1) {
		x = 0 + rand() % (15 - 0 + 1);
		y = 0 + rand() % (15 - 0 + 1);
		if (p[x][y] == '_') {
			p[x][y] = '@';
			break;
		}
	}

}

int Three_times_Three(int *arr, int x, int y) {
	int(*p)[HEIGHT] = arr;
	int sum = 0;
	for (int i = -1; i < 2; i++) {
		for (int j = -1; j < 2; j++) {
			if (p[x + i][y + j] == 'x') sum++;
		}
	}
	return sum;
}

int Scoring_Man1(int *arr) {
	int(*p)[HEIGHT] = arr;
	int Max_Hit = 0, temp = 0;
	for (int i = 1; i < WIDTH; i++) {
		for (int j = 0; j < HEIGHT; j++) {
			if (p[i][j] == 'x') {
				if (p[i + 1][j] == 'x') {
					temp = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j] == 'x')temp++;
					}
					if (temp > Max_Hit)Max_Hit = temp;
				}

				if (p[i][j + 1] == 'x') {
					temp = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i][j + k] == 'x')temp++;
					}
					if (temp > Max_Hit)Max_Hit = temp;
				}
				if (p[i + 1][j + 1] == 'x') {
					temp = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j + k] == 'x')temp++;
					}
					if (temp > Max_Hit)Max_Hit = temp;
				}
				if (p[i + 1][j - 1] == 'x') {
					temp = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j - k] == 'x')temp++;
					}
					if (temp > Max_Hit)Max_Hit = temp;
				}

			}

		}
	}

	return Max_Hit;
}
int Scoring_Man2(int *arr) {
	int(*p)[HEIGHT] = arr;
	int Max_Hit = 1, temp = 0;
	for (int i = 0; i < WIDTH; i++) {
		for (int j = 0; j < HEIGHT; j++) {
			if (p[i][j] == '*') {
				if (p[i + 1][j] == '*') {
					temp = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j] == '*')temp++;
					}
					if (temp > Max_Hit)Max_Hit = temp;
				}

				if (p[i][j + 1] == '*') {
					temp = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i][j + k] == '*')temp++;
					}
					if (temp > Max_Hit)Max_Hit = temp;
				}
				if (p[i + 1][j + 1] == '*') {
					temp = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j + k] == '*')temp++;
					}
					if (temp > Max_Hit)Max_Hit = temp;
				}
				if (p[i + 1][j - 1] == '*') {
					temp = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j - k] == '*')temp++;
					}
					if (temp > Max_Hit)Max_Hit = temp;
				}

			}

		}
	}

	return Max_Hit;
}

int Scoring_Robot(int *arr) {
	int(*p)[HEIGHT] = arr;
	int Max_Hit = 1, temp = 0;
	for (int i = 0; i < WIDTH; i++) {
		for (int j = 0; j < HEIGHT; j++) {
			if (p[i][j] == '@') {
				if (p[i + 1][j] == '@') {
					temp = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j] == '@')temp++;
					}
					if (temp > Max_Hit)Max_Hit = temp;
				}

				if (p[i][j + 1] == '@') {
					temp = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i][j + k] == '@')temp++;
					}
					if (temp > Max_Hit)Max_Hit = temp;
				}
				if (p[i + 1][j + 1] == '@') {
					temp = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j + k] == '@')temp++;
					}
					if (temp > Max_Hit)Max_Hit = temp;
				}
				if (p[i + 1][j - 1] == '@') {
					temp = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j - k] == '@')temp++;
					}
					if (temp > Max_Hit)Max_Hit = temp;
				}

			}

		}
	}

	return Max_Hit;
}
		

int Check_Man1(int *arr) {
	int(*p)[HEIGHT] = arr;
	int sum = 0;
	for (int i = 0; i < WIDTH; i++) {
		for (int j = 0; j < HEIGHT; j++) {
			if (p[i][j] == 'x') {
				if (p[i + 1][j] == 'x') {
					sum = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j] == 'x')sum++;
					}
					if (sum == 5)return Success_Man1;
				}
				
				if (p[i][j + 1] == 'x') {
					sum = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i][j + k] == 'x')sum++;
					}
					if (sum == 5)return Success_Man1;
				}
				if (p[i + 1][j + 1] == 'x') {
					sum = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j + k] == 'x')sum++;
					}
					if (sum == 5)return Success_Man1;
				}
				if (p[i + 1][j - 1] == 'x') {
					sum = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j - k] == 'x')sum++;
					}
					if (sum == 5)return Success_Man1;
				}
				
			}

		}
	}

	return Defeat;
}

int Check_Man2(int *arr) {
	int(*p)[HEIGHT] = arr;
	int sum = 0;
	for (int i = 0; i < WIDTH; i++) {
		for (int j = 0; j < HEIGHT; j++) {
			if (p[i][j] == '*') {
				if (p[i + 1][j] == '*') {
					sum = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j] == '*')sum++;
					}
					if (sum == 5)return Success_Man2;
				}

				if (p[i][j + 1] == '*') {
					sum = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i][j + k] == '*')sum++;
					}
					if (sum == 5)return Success_Man2;
				}
				if (p[i + 1][j + 1] == '*') {
					sum = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j + k] == '*')sum++;
					}
					if (sum == 5)return Success_Man2;
				}
				if (p[i + 1][j - 1] == '*') {
					sum = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j - k] == '*')sum++;
					}
					if (sum == 5)return Success_Man2;
				}

			}

		}
	}
	return Defeat;
}

int Check_Robot(int *arr) {
	int(*p)[HEIGHT] = arr;
	int sum = 0;
	for (int i = 0; i < WIDTH; i++) {
		for (int j = 0; j < HEIGHT; j++) {
			if (p[i][j] == '@') {
				if (p[i + 1][j] == '@') {
					sum = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j] == '@')sum++;
					}
					if (sum == 5)return Success_Robot;
				}

				if (p[i][j + 1] == '@') {
					sum = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i][j + k] == '@')sum++;
					}
					if (sum == 5)return Success_Robot;
				}
				if (p[i + 1][j + 1] == '@') {
					sum = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j + k] == '@')sum++;
					}
					if (sum == 5)return Success_Robot;
				}
				if (p[i + 1][j - 1] == '@') {
					sum = 0;
					for (int k = 0; k < 5; k++) {
						if (p[i + k][j - k] == '@')sum++;
					}
					if (sum == 5)return Success_Robot;
				}

			}

		}
	}
	return Defeat;
}

int Check_All(int *arr) {
	int(*p)[HEIGHT] = arr;
	if (Check_Man1(p) == Success_Man1) {
		puts("**************************\n");
		puts("*      Success_Man1      *\n");
		puts("**************************\n");
		return Finish;
	}
	if (Check_Man2(p) == Success_Man2) {
		puts("**************************\n");
		puts("*      Success_Man2      *\n");
		puts("**************************\n");
		return Finish;
	}
	if (Check_Robot(p) == Success_Robot) {
		puts("**************************\n");
		puts("*      Success_Robot     *\n");
		puts("**************************\n");
		return Finish;
	}
	return Keep;
}

int Check_Input(int *arr, int x, int y) {
	int(*p)[HEIGHT] = arr;
	if ((x < 0 || x > 15) && (y < 0 || y > 15)) {
		return Wrong;
	}
	if (p[x][y] != 95) {
		return Already_exists;
	}
	return Right;
}

void Shining_char(int *arr, int Winner) {
	int(*p)[HEIGHT] = arr;
	if (Winner == Man1) {
		for (int i = 0; i < WIDTH; i++) {
			for (int j = 0; j < HEIGHT; j++) {
				if (p[i][j] == 'x') {
					p[i][j] = 'o';
					Refresh(arr);
					Sleep(300);
				}
			}
		}
	}
	if (Winner == Man2) {
		for (int i = 0; i < WIDTH; i++) {
			for (int j = 0; j < HEIGHT; j++) {
				if (p[i][j] == '*') {
					p[i][j] = 'o';
					Sleep(300);
				}
			}
		}
	}
	if (Winner == Robot) {
		for (int i = 0; i < WIDTH; i++) {
			for (int j = 0; j < HEIGHT; j++) {
				if (p[i][j] == '@') {
					p[i][j] = 'o';
					Sleep(300);
				}
			}
		}
	}
}

void Clear() {
	system("cls");
}

void Refresh(int *arr) {
	int(*p)[HEIGHT] = arr;
	Clear();
	Print(p);
}

void Twinkle() {
	system("color 10");
	Sleep(300);
	system("color 20");
	Sleep(300);
	system("color 30");
	Sleep(300);
	system("color 40");
	Sleep(300);
	system("color 50");
	Sleep(300);
	system("color 60");
	Sleep(300);
}

void Time_Counter(int Minute_start, int Second_start) {
	time_t timep;
	struct tm *p;
	time(&timep);
	p = gmtime(&timep);
	int Minute_end = p->tm_min;
	int Second_end = p->tm_sec;
	int Minute_res = 0, Second_res = 0;
	Second_res = Second_end - Second_start;
	Minute_res = Minute_end - Minute_start;
	if (Second_res < 0) {
		Second_res += 60;
		Minute_res -= 1;
	}
	printf("Game duration is %d : %d \n", Minute_res, Second_res);
}